using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEngine;
using UnityEngine.UI;

public class UpdateClassName : MonoBehaviour
{
    public Text ClassAName;
    public Text ClassBName;

    public StudentManager studentManager;
    void Start()
    {
         // 如果没有指定StudentManager，则尝试查找
        if (studentManager == null)
        {
            studentManager = FindObjectOfType<StudentManager>();
            if (studentManager == null)
            {
                Debug.LogWarning("UpdateClassName: StudentManager not assigned or found!");
            }
        }
        StringEventSystem.Global.Register(EventID.GuanMing, (LiveCommentData comment) =>
        {
            UpdateClassNameFun(comment);
        }
        ).UnRegisterWhenGameObjectDestroyed(gameObject);
    }

    // Update is called once per frame
    void UpdateClassNameFun(LiveCommentData comment)
    {
        //先解析userid和冠名内容
         // 去除前后空格并转为大写
        string content = comment.content?.Trim();
        string newClassName = content;
        if (string.IsNullOrEmpty(content))
        {
            Debug.LogWarning("content is null or empty.");
        }
        else if (!content.StartsWith("冠名"))
        {
            Debug.LogWarning($"content does not start with '冠名': {content}");
        }       
        else
        {
            newClassName = content.Substring("冠名".Length);
        }
        
        int userId = comment.game_user.id;
        //判断是不是班级赛，不是则直接retuen
        if (PlayerPrefs.GetString(GameMode.Mode) != GameMode.Tuandui)
        {
            return;
        }
        //判断用户属于班级A还是B，根据userid查找
        GameObject studentNode = studentManager.GetStudentById(userId.ToString());
        if (studentNode == null)
        {
            return;
        }
         //获取classtype
        Student student = studentNode.GetComponentInChildren<Student>();
        int calssType = student.getClassType();
        if (calssType == 1)
        {
            ClassAName.text = newClassName;
        }
        else if (calssType == 2)
        {
            ClassBName.text = newClassName;
        }
    }
}
